package org.ppassantini.cham.core.security;

import java.util.ArrayList;
import java.util.List;

import org.dozer.Mapper;
import org.ppassantini.cham.core.business.security.dao.MenuDao;
import org.ppassantini.cham.core.business.security.dao.UsersDao;
import org.ppassantini.cham.core.business.security.dto.MenuDto;
import org.ppassantini.cham.core.business.security.dto.UserDto;
import org.ppassantini.cham.core.business.security.entity.Menu;
import org.ppassantini.cham.core.business.security.entity.Users;

/**
 * @author ppassantini
 * 
 */
public class SecurityManagerImpl implements SecurityManager {

	private UsersDao usersDao;
	private MenuDao menuDao;
	private Mapper mapper;

	/**
	 * @return
	 */
	public MenuDao getMenuDao() {
		return menuDao;
	}

	/**
	 * @param menuDao
	 */
	public void setMenuDao(MenuDao menuDao) {
		this.menuDao = menuDao;
	}

	/**
	 * @return
	 */
	public Mapper getMapper() {
		return mapper;
	}

	/**
	 * @param mapper
	 */
	public void setMapper(Mapper mapper) {
		this.mapper = mapper;
	}

	/**
	 * @return
	 */
	public UsersDao getUsersDao() {
		return usersDao;
	}

	/**
	 * @param usersDao
	 */
	public void setUsersDao(UsersDao usersDao) {
		this.usersDao = usersDao;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.ppassantini.cham.core.security.SecurityManager#login(java.lang.String
	 * , java.lang.String)
	 */
	@Override
	public Object login(String username, String password) {
		Users users = usersDao.verifyUser(username, password);

		return users != null ? mapper.map(users, UserDto.class) : null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.ppassantini.cham.core.security.SecurityManager#getMenuUser(java.lang
	 * .String)
	 */
	@Override
	public Object getMenuUser(String username) {
		List<Menu> list = menuDao.getMenuByUsername(username);
		List<MenuDto> listDto = new ArrayList<MenuDto>();

		for (Menu menu : list) {
			listDto.add(mapper.map(menu, MenuDto.class));
		}

		return listDto;
	}

}
